package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdut.material.mapper.InputMapper;
import com.sdut.material.mapper.RepositoryMapper;
import com.sdut.material.mapper.SupplierMaterialMapper;
import com.sdut.material.pojo.*;
import com.sdut.material.mapper.OutputMapper;
import com.sdut.material.pojo.vo.InputVO;
import com.sdut.material.pojo.vo.OutputVO;
import com.sdut.material.service.IMaterialService;
import com.sdut.material.service.IOutputService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.service.ISupplierMaterialService;
import com.sdut.material.service.ISupplierService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class OutputServiceImpl extends ServiceImpl<OutputMapper, Output> implements IOutputService {
    @Autowired
    private OutputMapper outputMapper;
    @Autowired
    private IMaterialService materialService;
    @Autowired
    private RepositoryMapper repositoryMapper;
    @Autowired
    private SupplierMaterialMapper supplierMaterialMapper;
    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        Long offset = (page - 1) * limit;
        List<Output> OutputList = outputMapper.selectByPage(offset,limit);
        int totalCount = outputMapper.selectTotalCount();
        List<OutputVO> outputVOList = new ArrayList<>();
        for (Output output : OutputList) {
            OutputVO outputVO = new OutputVO();
            Material material = materialService.selectById(output.getMaterialId());
            outputVO.setId(output.getId());
            outputVO.setMaterialName(material.getName());
            outputVO.setCount(output.getCount());
            outputVO.setShelflife(output.getShelflife());
            outputVO.setStatus(output.getStatus());
            outputVOList.add(outputVO);
        }
        return LayUITableResult.ok((long) totalCount,outputVOList);
    }

    @Override
    public void transmit(int materialId, int materialTotalCount) {
        //将传过来的参数进行保存，同时添加到出库单
        Output output = new Output();
        output.setMaterialId(materialId);
        output.setCount(materialTotalCount);
        output.setShelflife(600);
        output.setStatus(1);
        outputMapper.insert(output);
    }

    @Override
    public JSONResult outputRepository(Integer outputId) {
        Output output = outputMapper.selectById(outputId);
        //查出所有的供应商能提供这种物料的sumaId;
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("material_id",output.getMaterialId());
        List<SupplierMaterial> supplierMaterialList = supplierMaterialMapper.selectList(queryWrapper1);
        //将查出的sumaId传到仓库进行数量的减少
        int totalCount = output.getCount();
        for (SupplierMaterial supplierMaterial : supplierMaterialList) {
            if (totalCount == 0) {
                break;
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("suma_id",supplierMaterial.getId());
            Repository repository = repositoryMapper.selectOne(queryWrapper);
            if (repository.getCount() >= totalCount) {
                repository.setCount(repository.getCount() - totalCount);
                repositoryMapper.updateById(repository);
                totalCount = 0;
            } else {
                totalCount = totalCount - repository.getCount();
                repository.setCount(0);
                repositoryMapper.updateById(repository);
            }
        }
        output.setStatus(2);
        outputMapper.updateById(output);
        return JSONResult.ok("出库成功");
    }

    @Override
    public void cancelById(Integer outputId) {
        Output output = outputMapper.selectById(outputId);
        output.setStatus(3);
        outputMapper.updateById(output);
    }
}
